<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">

      <sticky :class-name="'sub-navbar published'" :v-index="1000">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">保存
        </el-button>
      </sticky>

      <div class="createPost-main-container">
        <el-row>

          <el-col :span="24">

            <div class="postInfo-container">
              <el-row>

                <el-col :span="8">
                  <el-form-item label-width="100px" label="名称:" class="postInfo-container-item">
                    <el-input v-model="postForm.name" auto-complete="off" size="mini"/>
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label-width="100px" label="描述:" class="postInfo-container-item">
                    <el-input :rows="1" v-model="postForm.description" type="textarea" class="article-textarea" autosize placeholder="请输入描述"/>
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label-width="100px" label="排序:" class="postInfo-container-item">
                    <el-input-number v-model="postForm.show_order" :min="1" :max="100" size="mini" />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="100px" label="状态" class="postInfo-container-item">
                    <el-select v-model="postForm.disabled" size="mini" placeholder="请选择">
                      <el-option v-for="item in stateOptions" :key="item.value" :label="item.label" :value="item.value"/>
                    </el-select>
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label-width="100px" label="" class="postInfo-container-item"/>
                </el-col>

                <el-col :span="8">
                  <el-form-item label-width="100px" label="" class="postInfo-container-item"/>
                </el-col>

              </el-row>

            </div>
          </el-col>
        </el-row>

      </div>
    </el-form>

  </div>
</template>

<script>
import Sticky from '@/components/Sticky' // 粘性header组件
import { fetchGroup, createGroup, updateGroup } from '@/api/group'

const defaultForm = {
  id: undefined,
  name: '',
  description: '',
  show_order: 1,
  disabled: 0
}

export default {
  name: 'GroupDetail',
  components: { Sticky },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: undefined
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(new Error(rule.field + '为必传项'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      postForm: Object.assign({}, defaultForm),
      menuItems: [],
      categoryOptions: [],
      stateOptions: [{ 'label': '有效', 'value': 0 }, { 'label': '无效', 'value': 1 }],
      rules: {
        name: [{ required: true, trigger: 'blur', validator: validateRequire }],
        url: [{ required: true, trigger: 'blur', validator: validateRequire }]
      }
    }
  },
  computed: {
    lang() {
      return this.$store.getters.language
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.currentId
      this.fetchData(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },
  methods: {
    fetchData(id) {
      fetchGroup(id).then(response => {
        this.postForm = response.data.data
      }).catch(err => {
        console.log(err)
      })
    },
    submitForm() {
      this.postForm.display_time = parseInt(this.display_time / 1000)
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          let requestFunc = createGroup
          let msg = '分组创建成功'
          if (this.postForm.id !== undefined && this.postForm.id > 1) {
            requestFunc = updateGroup
            msg = '分组修改成功'
          }
          requestFunc(this.postForm).then(response => {
            const r = response.data
            console.log(r)
            if (r.code === 0) {
              this.$notify({
                title: '成功',
                message: msg,
                type: 'success',
                duration: 2000
              })
              this.$emit('successGroup')
              this.postForm = Object.assign({}, defaultForm)
            } else {
              this.$message({ message: r.msg, type: 'error' })
            }
          })
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "~@/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
